package com.huluobo.day11_im.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;

public class NetUtils {

    private NetUtils() {
    }

    private static NetUtils netUtils = null;

    private static Context mContext;

    public static NetUtils getInstance(Context context) {
        if (netUtils == null) {
            netUtils = new NetUtils();
        }
        mContext = context;
        return netUtils;
    }

    //判断当前网络是否连接
    public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        return isConnected;
    }

    //判断当前网络速率是否大于100kps
    public boolean isSlowlyNetWork() {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
        if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
            // 这是一个蜂窝网络
            if (nc.getLinkUpstreamBandwidthKbps() > 100 && nc.getLinkDownstreamBandwidthKbps() > 100) {
                // 快,上下行速率超过100kps
                return false;
            } else {
                // 慢
                return true;
            }
        }
        return true;
    }
}
